<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">原密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqtext="原密码不能为空"
                       placeholder="原密码" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="new_password" lay-verify="required" lay-reqtext="新密码不能为空"
                       placeholder="新密码" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">再输入新密码</label>
            <div class="layui-input-block">
                <input type="password" name="check_new_password" lay-verify="required" lay-reqtext="原密码不能为空"
                       placeholder="再输入新密码" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"></label>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'iconPickerFa'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var miniPage = layui.miniPage;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data.field)
            if (data.field.new_password !== data.field.check_new_password ){
                layer.msg('输入的新密码不一致', {icon: 5})
                return false;
            }
            $.ajax({
                url: 'do-change-password',
                method: 'post',
                data: data.field,
                success: function (res) {
                    console.log(res)
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            // 关闭弹框
                            layer.close(parentIndex);
                            // 刷新列表
                            window.location.replace("{{url('/admin/login')}}")
                        })
                    } else {
                        layer.msg(res.msg, {icon: 5})
                    }
                },
                error: function (res) {
                    layer.msg('请求错误', {icon: 5})
                }
            });
            return false;
        });
    });
</script>
